$(document).ready(function() {
	initTemplate();
	get_comments();

	$("#nav_next").click(function() {
		nextStep();
	});
	$("#nav_back").click(function() {
		back();
	});

	$("#save_comment").click(function() {
		save_comment();
	});
});
/**
 * 初始化模板
 */
function initTemplate() {
	shouldShowbottom();
};
//判断编辑状态还是展示状态
function shouldShowbottom() {
	var showBottom = $("#static").val();
	var container = $("#container");
	var height = 0;
	if (showBottom == '') {
		height = $(window).height() - 47;
	} else {
		height = $(window).height();
	};
	container.height(height);
}
//保存成功后,返回
function nextStep() {
	var site_id = $('#site_id').val();
	var t_id = $('#t_id').val();
	var color = $('#color').val();
	window.location.href = "http://182.92.111.163/baisi/webroot/index.php/templates/set_item4?site_id=" + site_id + "&t_id=" + t_id + "&color=" + color;
	// window.location.href = "http://127.0.0.1/baisi/webroot/index.php/templates/set_item4?site_id="+site_id+"&t_id="+t_id+"&color="+color;

}
//返回上个界面
function back() {
	window.history.back();
}

function save_comment() {
	var site_id = $("#site_id").val();
	var contents = $("#comment").val();
	var star = 5;
	if (site_id && contents) {
		var url = "http://182.92.111.163/baisi/webroot/index.php/comments/save_comments";
		$.ajax({
			type: "POST",
			url: url,
			data: "site_id=" + site_id + "&star=" + star + "&contents=" + contents,
			async: false,
			success: function(data) {
				alert(data);
				get_comments()
			},
			error: function(data) {
				alert("评论失败");
			}
		});
	} else {
		alert("内容不能为空");
		return false;
	}
}

function get_comments() {
	var site_id = $("#site_id").val();
	if (site_id) {
		var url = "http://182.92.111.163/baisi/webroot/index.php/comments/get_comments";
		$.ajax({
			type: "POST",
			url: url,
			data: "site_id=" + site_id,
			async: false,
			success: function(data) {
				if (data != false) {
					var comment_list = $("#comment_list");
					comment_list.html('');
					var dataObj = eval("(" + data + ")");
					for (var i = 0; i < dataObj.length; i++) {
						comment_list.append("<div id='comment_item'>" + dataObj[i].contents + "</div>");
					}
				} else {
					$("#comment_list").append("<div id='comment_item'>当前没有评论内容</div>");
				}
			},
			error: function(data) {
				$("#comment_list").append("<div id='comment_item'>当前没有评论内容</div>");
			}
		});
	} else {
		$("#comment_list").append("<div id='comment_item'>菜不错，下次再来！</div>");
		$("#comment_list").append("<div id='comment_item'>菜不错，下次再来！</div>");
		$("#comment_list").append("<div id='comment_item'>菜不错，下次再来！</div>");
		$("#comment_list").append("<div id='comment_item'>菜不错，下次再来！</div>");
	}
}
